﻿

using QFramework;
using System.Linq;
using UnityEngine;

namespace ProjectChildhood
{
    /// <summary>
    /// 减少物体的命令类(命令设计模式)
    /// </summary>
    public class SwapItemCommand : AbstractCommand
    {

        private readonly int m_FromSlotIndex;
        private readonly int m_ToSlotIndex;

        public SwapItemCommand(int fromSlotIndex, int toSlotIndex)
        {
            m_FromSlotIndex = fromSlotIndex;
            m_ToSlotIndex = toSlotIndex;
        }

        protected override void OnExecute()
        {
            IToolBarSystem system = this.GetSystem<IToolBarSystem>();
            ToolbarSlot fromSlot = system.Slots[m_FromSlotIndex];
            ToolbarSlot toSlot = system.Slots[m_ToSlotIndex];

            string itemID = fromSlot.ItemName;
            int count = fromSlot.Count.Value;
            fromSlot.ItemName = toSlot.ItemName;
            fromSlot.Count.Value = -1; // 为了强行触发 Count的变更监听事件
            fromSlot.Count.Value = toSlot.Count;
            toSlot.ItemName = itemID;
            toSlot.Count.Value = -1;
            toSlot.Count.Value = count;
        }
    }
}
